//package com.anche.cywebnew.Utils.Shiro;
//
//import cn.hutool.cache.CacheUtil;
//import cn.hutool.cache.impl.TimedCache;
//import com.anche.cywebnew.DAO.configManager.SysOptionMapper;
//import com.anche.cywebnew.Service.configManager.SysOptionService;
//import com.anche.cywebnew.Utils.ResponseBody.ResponseResult;
//import com.anche.cywebnew.Utils.Spring.SpringBeanFactoryUtils;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.session.Session;
//import org.apache.shiro.subject.Subject;
//import org.apache.shiro.web.filter.AccessControlFilter;
//import org.apache.shiro.web.util.WebUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import javax.servlet.ServletRequest;
//import javax.servlet.ServletResponse;
//import javax.servlet.http.HttpServletResponse;
//import javax.xml.crypto.Data;
//import java.util.Calendar;
//import java.util.Date;
//import java.util.Objects;
//import java.util.concurrent.ConcurrentHashMap;
//
//public class TimeoutSessionControlFilter extends AccessControlFilter {
//
//    @Autowired
//    private SysOptionMapper optionMapper;
//
//    private String timeoutUrl; //踢出后到的地址
//
//    public void setTimeoutUrl(String timeoutUrl) {
//        this.timeoutUrl = timeoutUrl;
//    }
//
//    private TimedCache<String, String> timedCache = CacheUtil.newTimedCache(1000 * 60);
//
//    @Override
//    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
////        try {
////            HttpServletRequest request1 = (HttpServletRequest) request;
////            String requestHost = request1.getHeader("host");
////            if (requestHost != null && !ServerWhiteListUtil.isWhite(requestHost)) {
////                return Boolean.FALSE;
////            }
////            Subject token = SecurityUtils.getSubject();
////            Session session = token.getSession();
////            if (optionMapper == null) {
////                optionMapper = (SysOptionMapper) SpringBeanFactoryUtils.getBean(SysOptionMapper.class);
////            }
////            Date lastTime = (Date) session.getAttribute("lastTime");
////            String loginTimeOUt = optionMapper.getOptionValue("安全策略", "LoginTimeOut","1");
////            loginTimeOUt = loginTimeOUt == "" ? "18000" : loginTimeOUt;
////            Date lastAccess = (Date) session.getAttribute("lastTime");
////            if (Objects.isNull(lastAccess)){  // 第一次访问
////                lastAccess = new Date();
////                session.setAttribute("lastTime", lastAccess);
////            }
////
////            Calendar c = Calendar.getInstance();
////            c.setTime(lastAccess);
////            c.add(Calendar.SECOND, Integer.valueOf(loginTimeOUt));
////            Date outDate = c.getTime();
////            Date now = new Date();
////            if (!outDate.before(now)) {
////                session.setAttribute("lastTime", now);
////                return Boolean.TRUE;
////            }
////            session.removeAttribute("lastTime");
////            //如果是ajax请求响应头会有，x-requested-with 
//////            if (ShiroFilterUtils.isAjax(request)) {
//////                ResponseResult responseResult = new ResponseResult();
//////                responseResult.setCode("1");
//////                responseResult.setMessage("用戶长时间未操作，自动退出登录");
//////                ShiroFilterUtils.out((HttpServletResponse)response, null);
//////                //通过返回TRUE，通过前台的统一AJAX接受头部设置的sessionstatus参数，判断是否跳转到登录页面
//////                return Boolean.TRUE;
//////            }//FALSE  Session失效，切实非AJAX请求，验证是否，调用onAccessDenied跳转到登录页面
////            WebUtils.issueRedirect(request, response, "/toLogin");
////            Subject user = SecurityUtils.getSubject();
////            user.logout();
////            return Boolean.FALSE;
////        } catch (Exception e) {
////            e.printStackTrace();
////        }
//        return Boolean.TRUE;
//    }
//
//    @Override
//    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
//        return false;
//    }
//}
